<?php
namespace app\api\logic;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class SendMobile
{
    static protected $SignName ='天天云积分';// 必填，设置签名名称，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
    static protected $TemplateCode ='SMS_169640571';// 必填，设置模板CODE，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
    static protected $accessKeyId ='LTAIk7STSk5z2Um2';
    static protected $accessSecret ='HhW7MxsU0gTQ9KwUELgp151vuzRmEf';

    static public function send_mobile($code,$mobile){
        AlibabaCloud::accessKeyClient(self::$accessKeyId, self::$accessSecret)
            ->regionId('cn-hangzhou') // replace regionId as you need
            ->asDefaultClient();
        try {
            $result = AlibabaCloud::rpc()
                ->product('Dysmsapi')
                // ->scheme('https') // https | http
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->options([
                    'query' => [
                        'PhoneNumbers' => $mobile,
                        'SignName' => self::$SignName,
                        'TemplateCode' => self::$TemplateCode,
                        'TemplateParam' => json_encode(['code'=>$code], JSON_UNESCAPED_UNICODE),
                    ],
                ])
                ->request();
            $res = $result->toArray();
            if($res['Code'] == 'OK'){
                $data['status'] = 1;
            }else{
                $data['status'] = 2;
            }
            $data['msg'] = $res['Message'];
            return $data;
        } catch (ClientException $e) {
            $data=[
                'status'=>2,
                'msg'=>$e->getErrorMessage()
            ];
            return $data;
        } catch (ServerException $e) {
            $data=[
                'status'=>2,
                'msg'=>$e->getErrorMessage()
            ];
            return $data;
        }
    }
}
